package swing.frame.whiteboard;

import api.BoardServer;
import client.UserSession;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author ywjia
 */
public class DrawListener extends MouseAdapter implements ActionListener{

    MyDrawPanel myDrawPanel;

    private int x1,y1,x2,y2;
    private ToolsPanel tp;
    private ColorPanel cp;
    private MenuPanel mp;

    private List<String> onceDrawShapeList = new ArrayList<>();

    private DrawPanel dp;
    private Graphics2D g ;
    private Color color;
    private String tool;
    private String menu;
    private BufferedImage img;
    private BufferedImage image;
    private JTextField edit;
    public DrawListener(DrawPanel dp){g= (Graphics2D) dp.panel.getGraphics();}
    public DrawListener(ToolsPanel tp, ColorPanel cp, DrawPanel dp,MenuPanel mp, MyDrawPanel myDrawPanel) {
        g= (Graphics2D) dp.panel.getGraphics();
        this.tp = tp;
        this.cp = cp;
        this.dp = dp;
        this.mp = mp;
        this.myDrawPanel = myDrawPanel;
        color=cp.getbColor();
        tool="line";
    }

//    @Override
//    public void mouseClicked(MouseEvent e) {
//        //super.mouseClicked(e);
//        menu=mp.menu;
//        if("清空".equals(menu)){
//            try {
//                img= ImageIO.read(new File("image/White.png"));
//            } catch (IOException ex) {
//                ex.printStackTrace();
//            }
//            g.drawImage(img,0,0,800,600,null);
//            System.out.println("clear");
//        }
//
//    }

    @Override
    public void mousePressed(MouseEvent e) {
        //获得起点坐标以及颜色
        x1=e.getX();
        y1=e.getY();
        color=cp.getbColor();
        tool=tp.tool;
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        //获得鼠标终点的坐标
        x2=e.getX();
        y2=e.getY();
        //设置默认笔的粗细
        g.setStroke(new BasicStroke(2));
        g.setColor(color);

        if("line".equals(tool)){
            //画直线
            Shape shape = new Shape(Shape.LINE_TYPE, x1, y1, x2, y2, color, 2);
            shape.Draw(g);
            onceDrawShapeList.add(shape.toString());
        }

        if("rect".equals(tool)){
            //画矩形
            Shape shape = new Shape(Shape.RECT_TYPE, Math.min(x1,x2),Math.min(y1,y2),Math.abs(x2-x1),Math.abs(y2-y1), color, 2);
            shape.Draw(g);
            onceDrawShapeList.add(shape.toString());
            //g.drawRect(Math.min(x1,x2),Math.min(y1,y2),Math.abs(x2-x1),Math.abs(y2-y1));
        }

        if("roundrect".equals(tool)){

            //画圆角矩形
            Shape shape = new Shape(Shape.ROUND_RECT_TYPE, Math.min(x1,x2),Math.min(y1,y2),Math.abs(x2-x1),Math.abs(y2-y1), 10,10,color, 2);
            shape.Draw(g);
            onceDrawShapeList.add(shape.toString());
        }

        if("fillrect".equals(tool)){
            //画实心矩形
            Shape shape = new Shape(Shape.Fillrect_RECT_TYPE, Math.min(x1,x2),Math.min(y1,y2),Math.abs(x2-x1),Math.abs(y2-y1), color, 2);
            shape.Draw(g);
            onceDrawShapeList.add(shape.toString());
        }

        if("oval".equals(tool)){
            //画椭圆
            Shape shape = new Shape(Shape.OVAL_TYPE, Math.min(x1,x2),Math.min(y1,y2),Math.abs(x2-x1),Math.abs(y2-y1), color, 2);
            shape.Draw(g);
            onceDrawShapeList.add(shape.toString());

        }
        if("edit".equals(tool)){
            dp.textField.setBounds(x1-5, y1-10, 100, 30);
            dp.textField.addKeyListener(new KeyAdapter() {
                @Override
                public void keyPressed(KeyEvent e) {
                    if(e.getKeyCode()==KeyEvent.VK_ENTER){
                        e.consume();//屏蔽换行
                        String text = dp.textField.getText().trim();
                        dp.textField.setText("");
                        dp.textField.setVisible(false);
                        if (!text.isEmpty()) {
                            Shape shape = new Shape(Shape.TEXT_TYPE, x1, y1+10, color, text);
                            shape.Draw(g);
                            onceDrawShapeList.add(shape.toString());
                            onceDrawUpdate();
                        }
                    }
                }
            });
            SwingUtilities.invokeLater(() -> dp.textField.requestFocus());
            dp.textField.setVisible(true);
        }
        if("rhombus".equals(tool)){
            //画菱形
            Shape shape1 = new Shape(Shape.LINE_TYPE, x1, y1, x2, y2, color, 2);
            shape1.Draw(g);
            onceDrawShapeList.add(shape1.toString());
            Shape shape2 = new Shape(Shape.LINE_TYPE, x2, y2, x1+2*(x2-x1), y1, color, 2);
            shape2.Draw(g);
            onceDrawShapeList.add(shape2.toString());

            Shape shape3 = new Shape(Shape.LINE_TYPE, x1, y1, x2, 2*y1-y2, color, 2);
            shape3.Draw(g);
            onceDrawShapeList.add(shape3.toString());

            Shape shape4 = new Shape(Shape.LINE_TYPE, x2, 2*y1-y2, x1+2*(x2-x1), y1, color, 2);
            shape4.Draw(g);
            onceDrawShapeList.add(shape4.toString());
        }

        if("triangle".equals(tool)){
            //画三角形
            Shape shape1 = new Shape(Shape.LINE_TYPE, x1, y1, x2, y2, color, 2);
            shape1.Draw(g);
            onceDrawShapeList.add(shape1.toString());
            Shape shape2 = new Shape(Shape.LINE_TYPE, x2, y2, x1+2*(x2-x1), y1, color, 2);
            shape2.Draw(g);
            onceDrawShapeList.add(shape2.toString());
            Shape shape3 = new Shape(Shape.LINE_TYPE, x1, y1, x1+2*(x2-x1), y1, color, 2);
            shape3.Draw(g);
            onceDrawShapeList.add(shape3.toString());
        }


        onceDrawUpdate();
    }

    private static final int MAX_SEND_LEN = 30;
    private void onceDrawUpdate() {
        if (onceDrawShapeList.size() > 0) {
            dp.shapeList.addAll(onceDrawShapeList);
            String firstShapeStr = onceDrawShapeList.get(0) + "," + UserSession.UserAccount();
            onceDrawShapeList.set(0, firstShapeStr);
            List<String> list = new ArrayList<>(onceDrawShapeList);
            new Thread(() -> {
                int shapeSize = list.size();
                List<String> onceSendShapeList = new ArrayList<>();
                for(int i=0, toIndex=MAX_SEND_LEN;i<shapeSize;i=i+MAX_SEND_LEN,toIndex=toIndex+MAX_SEND_LEN) {
                    if (i + MAX_SEND_LEN > shapeSize) {
                        toIndex = shapeSize;
                    }
                    onceSendShapeList = list.subList(i, toIndex);
                    BoardServer.boardService.updateBoard(myDrawPanel.whiteboard.getId(), onceSendShapeList);
                }

            }).start();
            onceDrawShapeList.clear();
        }
    }


    @Override
    public void mouseDragged(MouseEvent e) {
        //获取鼠标终点坐标
        x2=e.getX();
        y2=e.getY();
        //设置默认笔的粗细

        if("pencil".equals(tool)){
            //铅笔
            Shape shape = new Shape(Shape.LINE_TYPE, x1, y1, x2, y2, color, 2);
            shape.Draw(g);
            onceDrawShapeList.add(shape.toString());
            x1=x2;
            y1=y2;
        }
        if("brush".equals(tool)){
            //画直线
            Shape shape = new Shape(Shape.LINE_TYPE, x1, y1, x2, y2, color, 10);
            shape.Draw(g);
            onceDrawShapeList.add(shape.toString());
            x1=x2;
            y1=y2;
        }

        if("erase".equals(tool)){
            //橡皮擦
            Shape shape = new Shape(Shape.LINE_TYPE, x1, y1, x2, y2, Color.WHITE, 20);
            shape.Draw(g);
            onceDrawShapeList.add(shape.toString());
            x1=x2;
            y1=y2;
        }

        if("spray".equals(tool)){
            //喷壶
            g.setStroke(new BasicStroke(1));
            g.setColor(color);
            Random random=new Random();
            for (int i = 0; i < 60; i++) {
                int w=random.nextInt(4);
                int p=random.nextInt(20);
                int q=random.nextInt(20);
                if(w==0){
                    Shape shape = new Shape(Shape.LINE_TYPE, x2+p,y2+q,x2+p,y2+q, color, 1);
                    shape.Draw(g);
                    onceDrawShapeList.add(shape.toString());
                }
                if(w==1){
                    Shape shape = new Shape(Shape.LINE_TYPE, x2-p,y2-q,x2-p,y2-q, color, 1);
                    shape.Draw(g);
                    onceDrawShapeList.add(shape.toString());
                }
                if(w==2){
                    Shape shape = new Shape(Shape.LINE_TYPE, x2+p,y2-q,x2+p,y2-q, color, 1);
                    shape.Draw(g);
                    onceDrawShapeList.add(shape.toString());
                }
                if(w==3){
                    Shape shape = new Shape(Shape.LINE_TYPE, x2-p,y2+q,x2-p,y2+q, color, 1);
                    shape.Draw(g);
                    onceDrawShapeList.add(shape.toString());
                }
            }
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
//        System.out.println("actionPerformed");
//        String buttonName = e.getActionCommand();
//        if("清空".equals(buttonName)){
//            Shape shape = new Shape(Shape.Clear_TYPE,0,0,1100,800,Color.WHITE,3);
//            shape.Draw(g);
//            onceDrawShapeList.add(shape.toString());
//            System.out.println("clear");
//        }
    }
}
